<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>webgl着色器</title>
  <script src="lib/webgl-debug.js"></script>
  <script src="lib/webgl-utils.js"></script>
  <script src="lib/cuon-utils.js"></script>
  <script src="lib/cuon-matrix.js"></script>
</head>

<body onload="main()">
  <canvas id="canvas" width="800" height="800"></canvas>
</body>
<script>
  // 顶点着色器伪代码
  var vShder_source = `
  attribute vec4 position;
  attribute vec4 color;
  uniform mat4 mat;
  varying vec4 col ;
void main(){
gl_Position = mat*position;
col=color;
}
`
  //片元着色器伪代码
  var fShder_source = `
  precision mediump float;
  varying vec4 col;
void main(){

  vec4 col1 ;
  float gray = col.r*0.589+col.g*0.299+col.b*0.144;
  col1.r =gray;
  col1.g =gray;
  col1.b =gray;
  col1.a =col.a;
  gl_FragColor = col1;
}
`
  function main() {
    var canvas = document.getElementById('canvas');
    //  获取上下文对象
    var gl = getWebGLContext(canvas);
    //  初始化着色器
    if (!initShaders(gl, vShder_source, fShder_source)) {
      return;
    }

    var n = initVertexBuffers(gl);


    // 指定一个覆盖canvas的颜色
    gl.clearColor(0, 0, 0, 1.0);
    var mat = gl.getUniformLocation(gl.program, "mat");
    draw(gl, n, mat);

  }

  function draw(gl, n, mat) {
    //设置视觉矩阵的相关信息（视点，视线，上方向
    var vm = new Matrix4();
    vm.setLookAt(0, 0, 5, 0, 0, -100, 0, 1, 0);

    //设置模型矩阵相关信息；
    var mm = new Matrix4();
    mm.setRotate(0, 0, 0, 1);

    //设置透视投影矩阵
    var pm = new Matrix4();
    pm.setPerspective(30, canvas.width / canvas.height, 1, 10);

    // var orthMarix = new Matrix4();
    // orthMarix.setOrtho(-1, 1, -1, 1, 0, 1);

    //计算出模型视图矩阵
    var vmm = pm.multiply(vm.multiply(mm));
    gl.uniformMatrix4fv(mat, false, vmm.elements);
    gl.clear(gl.COLOR_BUFFER_BIT);
    gl.drawArrays(gl.TRIANGLES, 0, n);

  }

  function initVertexBuffers(gl) {

    var vertexDatas = new Float32Array([

      // 右侧的三个三角形
      0.75, 1.0, -4.0, 0.4, 1.0, 0.4, // 后面的绿色
      0.25, -1.0, -4.0, 0.4, 1.0, 0.4,
      1.25, -1.0, -4.0, 1.0, 0.4, 0.4,

      0.75, 1.0, -2.0, 1.0, 1.0, 0.4, // 中间的黄色
      0.25, -1.0, -2.0, 1.0, 1.0, 0.4,
      1.25, -1.0, -2.0, 1.0, 0.4, 0.4,

      0.75, 1.0, 0.0, 0.4, 0.4, 1.0,  // 前面的蓝色
      0.25, -1.0, 0.0, 0.4, 0.4, 1.0,
      1.25, -1.0, 0.0, 1.0, 0.4, 0.4,

      // 左侧的三个三角形
      -0.75, 1.0, -4.0, 0.4, 1.0, 0.4, // 后面的绿色
      -1.25, -1.0, -4.0, 0.4, 1.0, 0.4,
      -0.25, -1.0, -4.0, 1.0, 0.4, 0.4,

      -0.75, 1.0, -2.0, 1.0, 1.0, 0.4, // 中间的黄色
      -1.25, -1.0, -2.0, 1.0, 1.0, 0.4,
      -0.25, -1.0, -2.0, 1.0, 0.4, 0.4,

      -0.75, 1.0, 0.0, 0.4, 0.4, 1.0,  // 前面的蓝色
      -1.25, -1.0, 0.0, 0.4, 0.4, 1.0,
      -0.25, -1.0, 0.0, 1.0, 0.4, 0.4


    ]);

    //创建缓冲区
    var vertexbuffer = gl.createBuffer();
    //绑定缓冲区
    gl.bindBuffer(gl.ARRAY_BUFFER, vertexbuffer);
    //向缓冲区写入数据
    gl.bufferData(gl.ARRAY_BUFFER, vertexDatas, gl.STATIC_DRAW);
    var size = vertexDatas.BYTES_PER_ELEMENT;
    var position = gl.getAttribLocation(gl.program, "position");
    var color = gl.getAttribLocation(gl.program, "color");

    gl.vertexAttribPointer(position, 3, gl.FLOAT, false, size * 6, 0);
    gl.enableVertexAttribArray(position);

    gl.vertexAttribPointer(color, 3, gl.FLOAT, false, size * 6, size * 3);
    gl.enableVertexAttribArray(color);

    return 18;
  }

  var gvs = `

attribute vec2 aVertexPosition;
attribute vec2 aTextureCoord;
uniform mat3 projectionMatrix;
varying vec2 vTextureCoord;
void main(void)
{
    gl_Position = vec4((projectionMatrix * vec3(aVertexPosition, 1.0)).xy, 0.0, 1.0);
    vTextureCoord = aTextureCoord;
}
`

  var gfs = `
varying vec2 vTextureCoord;
uniform sampler2D uSampler;
uniform float m[20];
uniform float uAlpha;
void main(void)
{
    vec4 c = texture2D(uSampler, vTextureCoord);

    if (uAlpha == 0.0) {
        gl_FragColor = c;
        return;
    }

    // Un-premultiply alpha before applying the color matrix. See issue #3539.
    if (c.a > 0.0) {
      c.rgb /= c.a;
    }

    vec4 result;

    result.r = (m[0] * c.r);
        result.r += (m[1] * c.g);
        result.r += (m[2] * c.b);
        result.r += (m[3] * c.a);
        result.r += m[4];

    result.g = (m[5] * c.r);
        result.g += (m[6] * c.g);
        result.g += (m[7] * c.b);
        result.g += (m[8] * c.a);
        result.g += m[9];

    result.b = (m[10] * c.r);
       result.b += (m[11] * c.g);
       result.b += (m[12] * c.b);
       result.b += (m[13] * c.a);
       result.b += m[14];

    result.a = (m[15] * c.r);
       result.a += (m[16] * c.g);
       result.a += (m[17] * c.b);
       result.a += (m[18] * c.a);
       result.a += m[19];

    vec3 rgb = mix(c.rgb, result.rgb, uAlpha);

    // Premultiply alpha again.
    rgb *= result.a;

    gl_FragColor = vec4(rgb, result.a);
}
`

</script>



</html>